﻿using System;

namespace Lesson_继承练习题
{
    //定义一个载具类、速度、最大速度、可乘人数、司机和乘客等，有上车、下车、行驶、车祸等方法
    //用载具类声明一个对象，并将若干人装载上车
    class Person
    {

    }
    class Driver : Person
    {

    }
    class Passenger : Person
    {

    }

    class Vehicle
    {
        private float speed;
        private float maxSpeed;
        //takeNum为当前人数 以persons.Length作为最大人数
        public int takeNum;
        private Person[] persons;

        public Vehicle(float speed, float maxSpeed, int takeNum)
        {
            this.speed = speed;
            this.maxSpeed = maxSpeed;
            this.takeNum = 0;
            persons = new Person[takeNum];
        }

        public void GetIn(Person p)
        {
            if (takeNum > persons.Length - 1)
            {
                return;
            }
            else
            {
                persons[takeNum] = p;
                takeNum++;
            }
        }

        public void GetOut(Person p)
        {
            int index = takeNum;
            for (int i = 0; i < takeNum - 1; i++)
            {
                if (persons[i] == p)
                {
                    for (int j = i; j < takeNum - 1; j++)
                    {
                        persons[j] = persons[j + 1];
                    }
                    persons[takeNum - 1] = null;
                    takeNum--;
                    break;
                }
            }
            if (takeNum == index)
            {
                Console.WriteLine("未找到要删除的对象");
            }
        }

        public void Moving()
        {

        }

        public void Accident()
        {

        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Vehicle v = new Vehicle(10, 20, 4);
            Person p1 = new Driver();
            Person p2 = new Passenger();
            v.GetIn(p1);
            v.GetIn(p1);
            v.GetIn(p2);
            v.GetIn(p2);

            v.GetOut(p1);
            v.GetOut(p1);
            v.GetOut(p1);
            Console.WriteLine(v.takeNum);
        }
    }
}